package Test;

import DB.*;
import Model.*;
import org.junit.Test;
import java.util.ArrayList;
import static org.junit.Assert.fail;

public class SessionDBTest {


    //OK!!!!
    @Test
    public void testInsertSession() throws Exception {
        SessionDB sessionDB = new SessionDB();
        TeacherDB teacherDB = new TeacherDB();
        TeamDB teamDB = new TeamDB();
        CourseDB courseDB = new CourseDB();

        Teacher teacher = teacherDB.findTeacherByID(25);
        Team team = teamDB.findTeam(1);
        Course course = courseDB.findCourse(1);

        int id = sessionDB.sessionGetMax();
        Session session = new Session();
        session.setId(id);
        session.setDate("2014-11-22");
        session.setTeacherId(teacher);
        session.setTeamId(team);
        session.setCourseId(course);

        int rc = sessionDB.insertSession(session);

        if (rc == 1) {
            System.out.println("Inserting session successful");
        }
        else {
            fail("Failed inserting session");
        }
    }
    //OK!!
    @Test
    public void testFindSession() throws Exception {
        SessionDB sessionDB = new SessionDB();
        int find = 1;

        Session session = sessionDB.findSession(find);

        if (session != null) {
            System.out.println(session.getId());
            System.out.println(session.getDate());
            System.out.println(session.getTeacherId().getId());
            System.out.println(session.getTeacherId().getInitials());
            System.out.println(session.getTeamId().getId());
            System.out.println(session.getTeamId().getName());
            System.out.println(session.getCourseId().getId());
            System.out.println(session.getCourseId().getName());

        }
        else {
            fail("Failed finding session - session is null");
        }
    }
    //OKAY
    @Test
    public void testFindSessionByStudentIdAndDate()
    {
        int studentId = 1;
        String date = "2013-09-02";
        StudentDB studentDB = new StudentDB();
        SessionDB sessionDB = new SessionDB();
        Session session = sessionDB.findSessionByStudentIdAndDate(studentDB.findStudentByID(studentId),date);

        if (session != null) {
            System.out.println(session.getId());
            System.out.println(session.getDate());
            System.out.println(session.getTeacherId().getId());
            System.out.println(session.getTeacherId().getInitials());
            System.out.println(session.getTeamId().getId());
            System.out.println(session.getTeamId().getName());
            System.out.println(session.getCourseId().getId());
            System.out.println(session.getCourseId().getName());

        }
        else {
            fail("Failed finding session - session is null");
        }
    }
    //Ok!
    @Test
    public void testGetSessionByTeacherAndDate()
    {
        int teacherId = 23;
        String date = "2013-09-02";
        SessionDB sessionDB = new SessionDB();
        TeacherDB teacherDB = new TeacherDB();
        Session session = sessionDB.getSessionByTeacherAndDate(teacherDB.findTeacherByID(teacherId),date);

        if (session != null) {
            System.out.println(session.getId());
            System.out.println(session.getDate());
            System.out.println(session.getTeacherId().getId());
            System.out.println(session.getTeacherId().getInitials());
            System.out.println(session.getTeamId().getId());
            System.out.println(session.getTeamId().getName());
            System.out.println(session.getCourseId().getId());
            System.out.println(session.getCourseId().getName());

        }
        else {
            fail("Failed finding session - session is null");
        }

    }
    //Okay!
    @Test
    public void testGetAllSessions() throws Exception {
        SessionDB sessionDB = new SessionDB();
        ArrayList<Session> sessionObj = sessionDB.getAllSessions(false);

        if (sessionObj != null) {
            for (Session s : sessionObj) {
                System.out.println(s.getId() + " " + s.getDate() + " " + s.getTeamId());
            }
        }
        else {
            fail("No sessions found");
        }
    }
    //OKAY
    @Test
    public void testGetAllSessionsBetweenDates() throws Exception {
        SessionDB sessionDB = new SessionDB();
        ArrayList<Session> sessionList = sessionDB.getAllSessionsBetweenDates("2013-09-02", "2013-09-15", 1, false);

        if (sessionList != null) {
            for (Session s : sessionList) {
                System.out.println(s.getId() + " " + s.getDate() + " " + s.getTeamId().getId());
            }
        }
        else {
            fail("No sessions found between date");
        }
    }
    //OKay
    @Test
    public void testUpdateSession() throws Exception {
        SessionDB sessionDB = new SessionDB();
        TeamDB teamDB = new TeamDB();
        TeacherDB teacherDB = new TeacherDB();
        CourseDB courseDB = new CourseDB();
        Teacher teacher = teacherDB.findTeacherByID(23);
        Team team = teamDB.findTeam(1);
        Course course = courseDB.findCourse(1);
        String updateValue = "2015-01-01";

        Session session = new Session();
        session.setId(2);
        session.setDate(updateValue);
        session.setTeacherId(teacher);
        session.setTeamId(team);
        session.setCourseId(course);

        sessionDB.updateSession(session);

        String sessionName = sessionDB.findSession(2).getDate();

        if (sessionName.equals(updateValue)) {
            System.out.println("Update successful");
        }
        else {
            fail("Update failed");
        }

    }
    //OKay
    @Test
    public void testDeleteSession() throws Exception {
        SessionDB sessionDB = new SessionDB();
        TeacherDB teacherDB = new TeacherDB();
        TeamDB teamDB = new TeamDB();
        CourseDB courseDB = new CourseDB();

        Teacher teacher = teacherDB.findTeacherByID(1);
        Team team = teamDB.findTeam(1);
        Course course = courseDB.findCourse(1);

        int deleteId = 200;
        Session session = new Session();
        session.setId(deleteId);
        session.setDate("2013-11-22");
        session.setTeacherId(teacher);
        session.setTeamId(team);
        session.setCourseId(course);
        sessionDB.insertSession(session);

        if (sessionDB.findSession(deleteId) != null) {

            sessionDB.deleteSession(deleteId);

            if (sessionDB.findSession(deleteId) == null) {
                System.out.println("Delete successful");
            }
            else {
                fail("Delete not successful");
            }
        }
        else {
            fail("Inserting for deletion failed");
        }
    }
}
